//
//  EmailOpenTableViewController.m
//  bitcoin
//
//  Created by caiyajun on 2018/6/6.
//  Copyright © 2018年 比特币. All rights reserved.
//

#import "EmailOpenTableViewController.h"
#import "LCGrayButton.h"
@interface EmailOpenTableViewController ()
@property (weak, nonatomic) IBOutlet UITextField *emailTF;
@property (weak, nonatomic) IBOutlet UITextField *emailPassTF;
@property (weak, nonatomic) IBOutlet LCGrayButton *emailCodeBtn;
@property (weak, nonatomic) IBOutlet UIButton *sureButton;

@end

@implementation EmailOpenTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrowleft-icon"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    self.navigationItem.leftBarButtonItem = back;
    
    
    [_emailTF setValue:[UIColor colorWithHexString:@"#616372"] forKeyPath:@"_placeholderLabel.textColor"];
    [_emailPassTF setValue:[UIColor colorWithHexString:@"#616372"] forKeyPath:@"_placeholderLabel.textColor"];
    
    
    [self.emailCodeBtn addTarget: self action:@selector(startTime:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.sureButton setBackgroundImage:[UIImage imageWithColor:RGBColor(86, 54, 157)] forState:(UIControlStateHighlighted)];
}
- (void)backAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)startTime:(UIButton *)sender{
    
    [self.view endEditing:YES];
    if (self.emailTF.text.length==0) {
        [MBProgressHUD showError:@"请输入帐号"];
        return;
    }
    if ( ![CheckUtil validateEmail:self.emailTF.text]) {
        [MBProgressHUD showError:@"请输入有效的邮箱地址!"];
        return;
    }
    [self changeShow];
    DebugLog(@"我点击了.... ");
    NSDictionary *param = @{@"accept_device" : self.emailTF.text,
                            @"site" : @"9"
                            };
    [LSRequestManager postRequestWithURL:HUOBI(@"api/auth/sendYzm")
                                  params:param
                                 success:^(NSDictionary *responseData) {
                                     if ([responseData[@"ret"] integerValue ] == 0) {
                                         
                                     } else {
                                         [MBProgressHUD showError:responseData[@"msg"]];
                                     }
                                 }
                                 failure:^(NSError *error) {
                                     
                                 }
                                 showHUD:NO];
    
    
    
    
    
    
    
    
    
    
    
}
-(void)changeShow{
    __block int timeout = 59; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self.emailCodeBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
                self.emailCodeBtn.enabled = YES;
                
            });
        }else{
            //            int minutes = timeout / 60;
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self.emailCodeBtn setTitle:[NSString stringWithFormat:@"重新获取%@秒",strTime] forState:UIControlStateNormal];
                self.emailCodeBtn.enabled = NO;
                
                
            });
            timeout--;
            
        }
    });
    dispatch_resume(_timer);
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 2;
}

- (IBAction)sureAction {
    [self.view endEditing:YES];
    if (self.emailTF.text.length==0) {
        [MBProgressHUD showError:@"请输入帐号"];
        return;
    }
    if ( ![CheckUtil validateEmail:self.emailTF.text]) {
        [MBProgressHUD showError:@"请输入有效的邮箱地址!"];
        return;
    }
    if (self.emailPassTF.text.length==0) {
        [MBProgressHUD showError:@"请输入验证码"];
        return;
    }
    NSDictionary *param= @{@"verify_type" : @"1",
                           @"device" : self.emailTF.text,
                           @"yzm" : self.emailPassTF.text
                           };
    [LSRequestManager putRequestWithURL:HUOBI(@"index.php/api/user/bindDevice")
                                 params:param
                                success:^(NSDictionary *responseData) {
                                    if ([responseData[@"ret"] integerValue ] == 0) {
                                        [UserDefaults setObject:self.emailTF.text forKey:K_EMAIL];
                                        [UserDefaults setObject:@"1" forKey:K_ISEMAIL];
                                        [UserDefaults synchronize];
                                        
                                        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeIsEmailStatue" object:nil];
                                        
                                        [self.navigationController popViewControllerAnimated:YES];
                                        
                                        
                                    } else {
                                        [MBProgressHUD showError:responseData[@"msg"]];
                                    }
                                }
                                failure:^(NSError *error) {
                                    
                                }
                                showHUD:NO];
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}


@end
